package com.yundingai.domain.entity;

import cn.hutool.core.util.IdUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.ai.chat.messages.*;

import java.util.List;
import java.util.Map;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class ChatMsg {
    //信息类型：是AI的，还是用户的，或者是系统的
    MessageType messageType;
    //对话的文本信息
    String text;
    //其他信息
    Map<String, Object> metadata;

    //将SpringAI的Message转为我们的Msg
    public ChatMsg(Message message,String createTime) {
        this.messageType = message.getMessageType();
        this.text = message.getText();
        message.getMetadata().put("createTime", createTime);
        message.getMetadata().put("id", IdUtil.createSnowflake(1, 1).nextIdStr());
        this.metadata = message.getMetadata();
    }

    //实现将我们的Msg转为SpringAI的Message
    public Message toMessage() {
        return switch (messageType) {
            case SYSTEM -> new SystemMessage(text);
            case USER -> new UserMessage(text, List.of(), metadata);
            case ASSISTANT -> new AssistantMessage(text, metadata, List.of(), List.of());
            default -> throw new IllegalArgumentException("Unsupported message type: " + messageType);
        };
    }
}